﻿

window.fbAsyncInit = function () {
    FB.init({
        appId: '393665767324914',
        status: true,
        cookie: true,
        xfbml: true,
        oauth: true
    });
    FB.getLoginStatus(function (response) {
        authenticate(response);
    });

    FB.Event.subscribe('auth.login', function (response) {
        logedIn(response);
    });
    FB.Event.subscribe('auth.logout', function (response) {
        logedOut(response);
    });
   
};
        (function(d){
           var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
           js = d.createElement('script'); js.id = id; js.async = true;
           js.src = "//connect.facebook.net/en_US/all.js";
           d.getElementsByTagName('head')[0].appendChild(js);
       } (document));

       function logedIn(response) {
           onSuccessfulLogin();
      //alert(response.authResponse.accessToken);
           
       }

       function logedOut(response) {
           clearSession();          
       }

       function authenticate(response) {
           if (response.status === 'connected') {
               var imgLink;
               FB.api('/me', function (user) {
                   if ($.isEmptyObject(user) == false) {                     
                       imgLink = "https://graph.facebook.com/" + user.id + "/picture?type=square";
                       var data = "{'First_Name' : '" + user.first_name.toString() + "', 'Last_Name' : '" + user.last_name.toString() + "', 'Email' : '" + user.email.toString() + "'}";
                       $("#profileImg").attr("src", imgLink);
                       CallHttpHandler("POST", "Handler/UserHandler.ashx", data, null, loginError);
                   }
               });
               //comment textbox on popup
               var cmt = $('.commentArea textarea');
               if ($.isEmptyObject(cmt) == false) {
                   cmt.removeAttr("disabled");
                   cmt.focus();
               }

               //comment textbox on profile page

               var pCmt = $('.profileCommentArea textarea');
               if ($.isEmptyObject(pCmt) == false) {
                   pCmt.removeAttr("disabled");                   
               }
               $(".commentFB").hide();
               $("#fbBtn").hide();
               $("#logedInUser").removeClass("display-none");    
               //set profile image               
           }
           else {
               cmt = $('.commentArea textarea')
               if ($.isEmptyObject(cmt) == false) {
                   cmt.attr("disabled", "disabled");
               }
               
               pCmt = $('.profileCommentArea textarea');
               if ($.isEmptyObject(pCmt) == false) {
                   pCmt.attr("disabled", "disabled");
               }
               $("#logedInUser").addClass("display-none");
               //set profile image
               $("#profileImg").attr("src", "");
               $("#fbBtn").show();
               $(".commentFB").show();
           }
       }
       function loginError(msg) {
           alert("Error occured!!! Please try again");
       }
       function clearSession() {
           CallHttpHandler("POST", "Handler/UserHandler.ashx", "{Email:'TimeOut'}", null, loginError);
       }
       function onSuccessfulLogin() {
           FB.getLoginStatus(function (response) {
               authenticate(response);
           });
           
       }
       function onLogout() {
           FB.logout(function (response) {
               $("#logedInUser").addClass("display-none");
               //set profile image
               $("#profileImg").attr("src", "");
               $("#fbBtn").show();
               FB._authResponse = null;
               FB._userStatus = null;
               document.cookie = 'fbsr_' + FB._apiKey + '=;'; // clear the facebook cookie

               var cmt = $('.commentArea textarea')
               if ($.isEmptyObject(cmt) == false) {
                   cmt.attr("disabled", "disabled");
               }
               var pCmt = $('.profileCommentArea textarea');
               if ($.isEmptyObject(cmt) == false) {
                   pCmt.attr("disabled", "disabled");
               }
           });
       
       }

     